package com.example.tpl.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.tpl.system.dao.OperationLogDao;
import com.example.tpl.system.entity.OperationLogBean;
import com.example.tpl.system.service.OperationLogService;
import com.example.tpl.system.util.Result;
import com.example.tpl.system.util.ResultPage;
import com.example.tpl.system.vo.common.DeleteBatchReqVo;
import com.example.tpl.system.vo.common.QueryDetailReqVo;
import com.example.tpl.system.vo.req.AddOperationLogReqVo;
import com.example.tpl.system.vo.req.QueryOperationLogReqVo;
import com.example.tpl.system.vo.resp.OperationLogRespVo;

/**
 * 描述：操作日志
 * 作者：刘飞华
 * 日期：2025/01/10 15:34:35
 */
@Service
public class OperationLogServiceImpl implements OperationLogService {

    @Autowired
    private OperationLogDao operationLogDao;


    /**
     * 添加操作日志
     *
     * @param operationLog 请求参数
     * @return int
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<Integer> addOperationLog(AddOperationLogReqVo operationLog) {
        OperationLogBean bean = new OperationLogBean();
        bean.setTitle(operationLog.getTitle()); //系统模块
        bean.setUserId(operationLog.getUserId()); //操作人员
        bean.setUserName(operationLog.getUserName()); //操作人员
        bean.setDeptId(operationLog.getDeptId()); //部门id
        bean.setDeptName(operationLog.getDeptName()); //部门名称
        bean.setPlatform(operationLog.getPlatform());//平台信息
        bean.setOs(operationLog.getOs());//操作系统
        bean.setBrowser(operationLog.getBrowser());//浏览器类型
        bean.setVersion(operationLog.getVersion());//浏览器版本
        bean.setEngine(operationLog.getEngine());//渲染引擎信息
        bean.setOperateIp(operationLog.getOperateIp()); //IP地址
        bean.setOperationUrl(operationLog.getOperationUrl()); //请求URL
        bean.setRequestMethod(operationLog.getRequestMethod()); //请求方式
        bean.setOperationDesc(operationLog.getOperationDesc()); //操作描述
        bean.setStatus(operationLog.getStatus()); //操作状态(0:异常,正常)
        bean.setOperateParam(operationLog.getOperateParam()); //请求参数
        bean.setJsonResult(operationLog.getJsonResult()); //操作结果
        bean.setErrorMsg(operationLog.getErrorMsg()); //异常消息
        bean.setErrMsgDetail(operationLog.getErrMsgDetail()); //详细的异常信息
        bean.setCostTime(operationLog.getCostTime()); //耗时
        bean.setOperateTime(operationLog.getOperateTime()); //操作时间

        int i = operationLogDao.insert(bean);
        return Result.success(i);
    }

    /**
     * 删除操作日志
     *
     * @param operationLog 请求参数
     * @return int
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<Integer> deleteOperationLog(DeleteBatchReqVo operationLog) {
        int i = operationLogDao.deleteByIds(operationLog.getIds());
        return Result.success(i);
    }

    /**
     * 清空操作日志
     *
     * @return int
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<Integer> cleanOperationLog() {
        int i = operationLogDao.delete(null);
        return Result.success(i);
    }


    /**
     * 查询操作日志详情
     *
     * @param operationLog 请求参数
     * @return OperationLogResp
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<OperationLogRespVo> queryOperationLogDetail(QueryDetailReqVo operationLog) {

        OperationLogBean operationLogBean = operationLogDao.selectById(operationLog.getId());
        if (operationLogBean == null) {
            return Result.error("操作日志不存在");
        }


        OperationLogRespVo resp = new OperationLogRespVo();
        resp.setId(operationLogBean.getId());//主键
        resp.setTitle(operationLogBean.getTitle());//系统模块
        resp.setUserId(operationLogBean.getUserId());//操作人员
        resp.setUserName(operationLogBean.getUserName());//操作人员
        resp.setDeptId(operationLogBean.getDeptId());//部门id
        resp.setDeptName(operationLogBean.getDeptName());//部门名称
        resp.setPlatform(operationLogBean.getPlatform());//平台信息
        resp.setOs(operationLogBean.getOs());//操作系统
        resp.setBrowser(operationLogBean.getBrowser());//浏览器类型
        resp.setVersion(operationLogBean.getVersion());//浏览器版本
        resp.setEngine(operationLogBean.getEngine());//渲染引擎信息
        resp.setOperateIp(operationLogBean.getOperateIp());//IP地址
        resp.setOperationUrl(operationLogBean.getOperationUrl());//请求URL
        resp.setRequestMethod(operationLogBean.getRequestMethod());//请求方式
        resp.setOperationDesc(operationLogBean.getOperationDesc());//操作描述
        resp.setStatus(operationLogBean.getStatus());//操作状态(0:异常,正常)
        resp.setOperateParam(operationLogBean.getOperateParam());//请求参数
        resp.setJsonResult(operationLogBean.getJsonResult());//操作结果
        resp.setErrorMsg(operationLogBean.getErrorMsg());//异常消息
        resp.setErrMsgDetail(operationLogBean.getErrMsgDetail());//详细的异常信息
        resp.setCostTime(operationLogBean.getCostTime());//耗时
        resp.setOperateTime(operationLogBean.getOperateTime());//操作时间

        return Result.success(resp);
    }

    /**
     * 查询操作日志列表
     *
     * @param operationLog 请求参数
     * @return OperationLogResp
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<ResultPage<OperationLogRespVo>> queryOperationLogList(QueryOperationLogReqVo operationLog) {
        LambdaQueryWrapper<OperationLogBean> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(operationLog.getUserId() != null, OperationLogBean::getUserId, operationLog.getUserId());
        queryWrapper.like(operationLog.getUserName() != null, OperationLogBean::getUserName, operationLog.getUserName());
        queryWrapper.eq(operationLog.getOperationStatus() != null, OperationLogBean::getStatus, operationLog.getOperationStatus());

        Page<OperationLogBean> page = operationLogDao.selectPage(new Page<>(operationLog.getPageNo(), operationLog.getPageSize()), queryWrapper);

        List<OperationLogRespVo> list = page.getRecords().stream().map(x -> {
            OperationLogRespVo resp = new OperationLogRespVo();
            resp.setId(x.getId());//主键
            resp.setTitle(x.getTitle());//系统模块
            resp.setUserId(x.getUserId());//操作人员
            resp.setUserName(x.getUserName());//操作人员
            resp.setDeptId(x.getDeptId());//部门id
            resp.setDeptName(x.getDeptName());//部门名称
            resp.setPlatform(x.getPlatform());//平台信息
            resp.setOs(x.getOs());//操作系统
            resp.setBrowser(x.getBrowser());//浏览器类型
            resp.setVersion(x.getVersion());//浏览器版本
            resp.setEngine(x.getEngine());//渲染引擎信息
            resp.setOperateIp(x.getOperateIp());//IP地址
            resp.setOperationUrl(x.getOperationUrl());//请求URL
            resp.setRequestMethod(x.getRequestMethod());//请求方式
            resp.setOperationDesc(x.getOperationDesc());//操作描述
            resp.setStatus(x.getStatus());//操作状态(0:异常,正常)
            resp.setOperateParam(x.getOperateParam());//请求参数
            resp.setJsonResult(x.getJsonResult());//操作结果
            resp.setErrorMsg(x.getErrorMsg());//异常消息
            resp.setErrMsgDetail(x.getErrMsgDetail());//详细的异常信息
            resp.setCostTime(x.getCostTime());//耗时
            resp.setOperateTime(x.getOperateTime());//操作时间
            return resp;
        }).collect(Collectors.toList());

        return Result.success(new ResultPage<>(list, page.getCurrent(), page.getSize(), page.getTotal()));

    }
}